﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.Threading;

namespace JasRibbonApplicatie.Classes
{
    class Emailer
    {


        //config
        private NetworkCredential credentials;
        public string Host = "127.0.0.1";
        public int Port = 25;

        public bool UseSSL = true;
        public int Timeout = 60000;
        public bool IsHtml = true;
        public SmtpDeliveryMethod SendUsing = SmtpDeliveryMethod.Network;//Network, PickupDirectory, SpecifiedPickupDirectory
        
        public int AuthenticationMode = 1;//0 = No authentication, 1 = Plain Text, 2 = NTLM authentication

        //message
        public MailAddress From = null;

        public Emailer(string host, int port, string user="", string password="")
        {
            Host = host;
            Port = port;
            if(user=="" && password=="") AuthenticationMode = 0;
            else credentials = new NetworkCredential(user, password);
        }

        /// <summary>
        /// Send Email Message method.
        /// </summary>
        public void SendMessage(string to, string subject, string body, string[] attachmentPaths, string cc = "", string bcc = "")
        {
            try
            {
                MailMessage oMessage = new MailMessage();
                SmtpClient smtpClient = new SmtpClient(Host,Port);
                
                #region mail message
                oMessage.From = From;
                var addresses = getMailAddressesFromString(to);
                foreach (var address in addresses) oMessage.To.Add(address);
                oMessage.Subject = subject;
                oMessage.IsBodyHtml = IsHtml;
                oMessage.Body = body;

                if (cc != "")
                {
                    addresses = getMailAddressesFromString(cc);
                    foreach (var address in addresses) oMessage.CC.Add(address);
                }
                if (bcc != "")
                {
                    addresses = getMailAddressesFromString(bcc);
                    foreach (var address in addresses) oMessage.Bcc.Add(address);
                }

                // Create and add the attachment
                if (attachmentPaths.Count() > 0)
                {
                    foreach (var attachmentPath in attachmentPaths)
                        oMessage.Attachments.Add(new Attachment(attachmentPath));
                }

                oMessage.BodyEncoding = UTF8Encoding.UTF8;
                oMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                #endregion

                #region setup smtpClient
                smtpClient.EnableSsl = UseSSL;
                smtpClient.Timeout = Timeout;
                smtpClient.DeliveryMethod = SendUsing;
                smtpClient.UseDefaultCredentials = false;
                if (AuthenticationMode > 0)
                {
                    smtpClient.Credentials = credentials;
                }
                #endregion

                try
                {
                    // Deliver the message    
                    smtpClient.Send(oMessage);

                }
                catch (Exception ex)
                {
                    Console.WriteLine("smtp.cs sendMessage exeption:" + ex.ToString());

                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("smtp.cs sendMessage exeption:" + ex.ToString());
            }
        }
        private MailAddressCollection getMailAddressesFromString(string mailstring)
        {
            MailAddressCollection mailaddresses = new MailAddressCollection();
            var toArray = mailstring.Split(';');
            if (toArray.Length > 0)
                foreach (string mail in toArray)
                {
                    if (mail.Contains('<') && mail.Contains('>'))
                    {
                        string email = mail.Substring(mail.IndexOf('<') + 1, mail.Length - mail.IndexOf('<') - 2);
                        string name = mail.Substring(0, mail.IndexOf('<') - 1);
                        mailaddresses.Add(new MailAddress(email, name));
                    }
                    else
                    {
                        if (mail != "") mailaddresses.Add(new MailAddress(mail));
                    }
                }
            else mailaddresses.Add(new MailAddress(mailstring));
            return mailaddresses;
        }
    }
}